package com.heartline.service;

import com.blade.mvc.annotation.*;
import com.blade.mvc.http.Request;
import com.heartline.api.IWXServer;
import com.soecode.wxtools.api.IService;
import com.soecode.wxtools.api.WxService;
import com.soecode.wxtools.bean.WxXmlMessage;
import com.soecode.wxtools.util.xml.XStreamTransformer;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

/**
 * 微信服务器接入
 * Created by Administrator on 2018/6/3 0003.
 */
@Slf4j
@Path("wx")
public class WXServer implements IWXServer {

    private IService iService = new WxService();

    @GetRoute()
    @TEXT
    @Override
    public String check(@Param String signature, @Param String timestamp, @Param String nonce, @Param String echostr, Request request) {
        if (iService.checkSignature(signature, timestamp, nonce, echostr)) {
            return echostr;
        }
        return "fail";
    }

    @PostRoute
    @TEXT
    @Override
    public String receive(Request request) throws IOException {
        // 微信服务器推送过来的是XML格式。
        WxXmlMessage wx = XStreamTransformer.fromXml(WxXmlMessage.class, request.bodyToString());
        log.info("消息： " + wx.toString());
        return "success";
    }
}
